﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using BEELab.Data;

namespace BEELab.Logic.Execution
{
    public class SessionRecovery
    {
        public static void SaveSessionVariable(int SessionID, string Name, string Value)
        {
            SaveSubjectVariable(SessionID, "0", Name, Value);
        }

        public static void SaveSubjectVariable(int SessionID, string SubjectID, string Name, string Value)
        {
            SqlCommand command = new SqlCommand();
            command.CommandText = "UPDATE SessionRecovery SET Value=@Value WHERE Name=@Name AND SubjectID=@SubjectID AND SessionID=@SessionID";
            command.Parameters.Add(new SqlParameter("@SessionID", SessionID));
            command.Parameters.Add(new SqlParameter("@SubjectID", SubjectID));
            command.Parameters.Add(new SqlParameter("@Name", Name));
            command.Parameters.Add(new SqlParameter("@Value", Value));

            int res = DBHelper.ExecuteNonQuery(command);

            if (res > 0)
                return;

            command = new SqlCommand();
            command.CommandText = "INSERT INTO SessionRecovery (SessionID, SubjectID, Name, Value) VALUES (@SessionID, @SubjectID, @Name, @Value)";
            command.Parameters.Add(new SqlParameter("@SessionID", SessionID));
            command.Parameters.Add(new SqlParameter("@SubjectID", SubjectID));
            command.Parameters.Add(new SqlParameter("@Name", Name));
            command.Parameters.Add(new SqlParameter("@Value", Value));

            res = DBHelper.ExecuteNonQuery(command);
        }

        public static void RecoverSession(int SessionID)
        {
            Logic.Execution.Execute.EstablishSession(SessionID);

            SqlCommand command = new SqlCommand();
            command.CommandText = "SELECT SubjectID, Name, Value FROM SessionRecovery WHERE SessionID=@SessionID";
            command.Parameters.Add(new SqlParameter("@SessionID", SessionID));
            DataTable dt = DBHelper.ExecuteQuery(command);

            foreach (DataRow row in dt.Rows)
            {
                if (row["SubjectID"].ToString() == "0")
                {
                    if (Global.SessionVariables[SessionID].ContainsKey(row["Name"].ToString()))
                    {
                        Global.SessionVariables[SessionID][row["Name"].ToString()].Value = row["Value"].ToString();
                    }
                    else
                    {
                        Global.SessionVariables[SessionID][row["Name"].ToString()] = new ProgramManagement.VariableManagement.VariableManagement.Variable(row["Name"].ToString(), "", "Session", row["Value"].ToString());
                        Global.SessionVariables[SessionID][row["Name"].ToString()].SessionID = SessionID;
                        Global.SessionVariables[SessionID][row["Name"].ToString()].SubjectID = "0";
                    }
                }
                else
                {
                    if (Global.SubjectVariables.ContainsKey(row["SubjectID"].ToString()) == false)
                    {
                        Global.SubjectVariables[row["SubjectID"].ToString()] = new Dictionary<string, ProgramManagement.VariableManagement.VariableManagement.Variable>();
                    }

                    if (Global.SubjectVariables[row["SubjectID"].ToString()].ContainsKey(row["Name"].ToString()))
                    {
                        Global.SubjectVariables[row["SubjectID"].ToString()][row["Name"].ToString()].Value = row["Value"].ToString();
                    }
                    else
                    {
                        Global.SubjectVariables[row["SubjectID"].ToString()][row["Name"].ToString()] = new ProgramManagement.VariableManagement.VariableManagement.Variable(row["Name"].ToString(), "", "Subject", row["Value"].ToString());
                        Global.SubjectVariables[row["SubjectID"].ToString()][row["Name"].ToString()].SessionID = SessionID;
                        Global.SubjectVariables[row["SubjectID"].ToString()][row["Name"].ToString()].SubjectID = row["SubjectID"].ToString();
                    }
                }
            }
        }

        public static void DropSession(int SessionID)
        {
            SqlCommand command = new SqlCommand();
            command.CommandText = "DELETE FROM SessionRecovery WHERE SessionID=@SessionID";
            command.Parameters.Add(new SqlParameter("@SessionID", SessionID));

            DBHelper.ExecuteNonQuery(command);
        }

        public static List<int> GetStoredSessionsList()
        {
            List<int> res = new List<int>();

            SqlCommand command = new SqlCommand();
            command.CommandText = "SELECT DISTINCT SessionID FROM SessionRecovery";
            DataTable dt = DBHelper.ExecuteQuery(command);

            foreach (DataRow row in dt.Rows)
            {
                res.Add(int.Parse(row["SessionID"].ToString()));
            }

            return res;
        }

        public static void RecoverAllSessions()
        {
            List<int> Sessions = GetStoredSessionsList();

            foreach (int sessionID in Sessions)
            {
                RecoverSession(sessionID);
            }
        }
    }
}